package data;

import java.util.Random;

// 农田地图类
public class FieldMap {
    private final int width;
    private final int height;
    private final String[][] grid;
    private final String[][] conditions; // 土壤条件
    private static final Random random = new Random();
    public FieldMap(int width, int height) {
        this.width = width;
        this.height = height;
        this.grid = new String[height][width];
        this.conditions = new String[height][width];
        initializeGrid();
    }

    private void initializeGrid() {
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                grid[i][j] = "□"; // 空地
                conditions[i][j] = "正常";
            }
        }
    }

    // 生成随机农田状况
    public void generateRandomConditions() {
        String[] conditionTypes = {"正常", "湿润", "干旱", "肥沃", "贫瘠"};

        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                conditions[i][j] = conditionTypes[random.nextInt(conditionTypes.length)];
            }
        }
    }

    // 添加障碍物
    public void addObstacle(int x, int y) {
        if (x >= 0 && x < width && y >= 0 && y < height) {
            grid[y][x] = "■"; // 障碍物
        }
    }

    // 显示地图
    public void displayMap() {
        System.out.println("\n农田地图 (■=障碍物):");
        System.out.print("    ");
        for (int i = 0; i < width; i++) {
            System.out.print((char)('A' + i) + "   ");
        }
        System.out.println();

        for (int i = 0; i < height; i++) {
            System.out.print((i + 1) + " | ");
            for (int j = 0; j < width; j++) {
                System.out.print(grid[i][j] + " | ");
            }
            System.out.println("\n  ---------------------------------");
        }

        System.out.println("\n图例: □=可通行区域, ■=障碍物");
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public boolean isPassable(int x, int y) {
        return grid[y][x].equals("□");
    }

    public String getCondition(int x, int y) {
        return conditions[y][x];
    }
}